package com.imooc;

import com.imooc.filter.AuthFilter;
import com.imooc.filter.DemoFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

/**
 * @describte:用于配置Route
 * @program:foodie-cloud
 * @author:zhengdp
 * @create:2021-10-07 00:14:06
 **/
@Configuration
public class RoutesConfiguration {

    @Autowired
    private KeyResolver hostNameResolver;

    @Autowired
    @Qualifier("redisLimiterUser")
    private RedisRateLimiter rateLimiterUser;

//    @Autowired
//    private AuthFilter authFilter;


    @Bean
    public RouteLocator routes(RouteLocatorBuilder builder,AuthFilter authFilter){
        return builder.routes()
                .route(r->r.path("/auth-service/refresh")
                .uri("lb://FOODIE-AUTH-SERVICE")
                )
                .route(r -> r.path("/address/list",
                        "/address/add",
                        "/address/update",
                        "/address/setDefalut",
                        "/address/delete",
                        "/userInfo/**", "/center/**")
                        .filters(f -> f.filter(authFilter))
                        .uri("lb://FOODIE-USER-SERVICE")
                )
                .route(r -> r.path("/address/**","/passport/**",
                        "/userInfo/**","/center/**")
                        .filters(f-> f.requestRateLimiter(c->{
                            c.setKeyResolver(hostNameResolver);
                            c.setRateLimiter(rateLimiterUser);
                            c.setStatusCode(HttpStatus.BAD_GATEWAY);
                        }
                        ))
                        .uri("lb://FOODIE-USER-SERVICE")
                )
                .route(r -> r.path("/items/**")
                .uri("lb://FOODIE-ITEM-SERVICE")
                )
                .route(r -> r.path("/shopcart/**")
                        .uri("lb://FOODIE-CART-SERVICE")
                )
                .route(r -> r.path("/orders/**","/myorders/**",
                        "/mycomments/**")
                        .uri("lb://FOODIE-ORDER-SERVICE")
                )
                .build();
    }
}
